package com.lanou3g.dllo.baidumusic.search;

import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.BaseFragment;
import com.lanou3g.dllo.baidumusic.event.IndexEvent;
import com.lanou3g.dllo.baidumusic.main.playmusic.PlaymusicBean;
import com.lanou3g.dllo.baidumusic.event.SongIdEvent;
import com.lanou3g.dllo.baidumusic.tools.MyVolley;
import com.lanou3g.dllo.baidumusic.tools.PlaylistManager;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by dllo on 16/7/2.
 */

public class SearchFragment extends BaseFragment {

    private SearchAdapter searchAdapter;
    private ListView searchListView;
    private EditText searchContent;
    private SearchBean searchBean;
    private ImageView searchClickSearch, searchReturnBtn;

    @Override
    public int setLayout() {
        return R.layout.fragment_search;
    }

    @Override
    public void initView(View view) {
        searchBean = new SearchBean();
        searchAdapter = new SearchAdapter(context);
        searchListView = (ListView) view.findViewById(R.id.search_listview);
        searchContent = (EditText) view.findViewById(R.id.search_content);
        searchClickSearch = (ImageView) view.findViewById(R.id.search_clicksearch);
        searchReturnBtn = (ImageView) view.findViewById(R.id.search_return_btn);
        // 返回键
        searchReturnBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentByTag("SearchFragment")).commit();
                getFragmentManager().beginTransaction().show(getFragmentManager().findFragmentByTag("MainFragment")).commit();
                searchContent.setText("");
            }
        });

        searchListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String songId = searchBean.getResult().getSong_info().getSong_list().get(position).getSong_id();
                EventBus.getDefault().post(new SongIdEvent(songId));
                EventBus.getDefault().post(new IndexEvent(position));
                PlaylistManager.getInstance().getPlaylist().clear();
                // 让播放列表获得歌单里的全部歌曲
                for (SearchBean.ResultBean.SongInfoBean.SongListBean bean : searchBean.getResult().getSong_info().getSong_list()) {
                    PlaylistManager.getInstance().getPlaylist().add(new PlaymusicBean(bean.getSong_id()
                            , bean.getAuthor(), bean.getTitle()));
                }
            }
        });
    }

    @Override
    public void initData() {
        searchClickSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (searchContent.getText().toString().isEmpty()) {
                    Toast.makeText(context, "请输入要搜索的内容", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.search.merge&query=" + searchContent.getText().toString() + "&page_size=50&page_no=1&type=-1&format=json&from=ios&version=5.2.5&from=ios&channel=appstore";
                    MyVolley.getMyVolley().initGson(url, new MyVolley.GetGsonListener<SearchBean>() {
                        @Override
                        public void getGsonListener(SearchBean data) {
                            searchBean = data;
                            searchAdapter.setSearchBean(data);
                            searchListView.setAdapter(searchAdapter);
                            searchContent.setText("");
                        }
                    }, SearchBean.class);
                }
            }
        });
    }

}
